﻿using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using VideoClipNameHelper;

namespace ClipPlayer
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private bool isBetweenTrack = false;
        //====================================================================================================================
        private void win1_PreviewKeyUp(object sender, KeyEventArgs e)
        {
            switch(e.Key)
            {
                case Key.Space:
                    buttonPlay_Click(null, null);
                    break;
                case Key.Left:
                    buttonPrev_Click(null, null);
                    break;
                case Key.Right:
                    buttonNext_Click(null, null);
                    break;
                case Key.Tab:
                    buttonRandom_Click(null, null);
                    break;
                case Key.Delete:
                    buttonDelete_Click(null, null);
                    break;
                case Key.OemMinus:
                case Key.Subtract:
                    buttonNote_Click(null, null);
                    break;
                case Key.Return:
                    buttonLike_Click(null, null);
                    break;
                case Key.OemPlus:
                case Key.Add:
                    break;
            }
            e.Handled = true;
        }
        //====================================================================================================================
        private void buttonPrev_Click(object sender, RoutedEventArgs e)
        {
            Button b = sender as Button;
            //
            if (null != b) b.IsEnabled = false;
            int toSkip = currentPlaying;
            //
            if (0 != md.MediaFiles.Count())
            {
                if (currentPlaying > 0)
                {
                    wmp.VolumeToZero();
                    isBetweenTrack = true;
                    //
                    stopPlaying();
                    currentPlaying--;
                    playCurrent();
                    //
                    isBetweenTrack = false;
                    //wmp.VolumeToMaxAsync();
                }
                else
                {
                    stopPlaying();
                }
                System.Threading.Tasks.Task.Run(() => { return VideoClipNameHelper.CDBHandler.UpdateData(VideoClipData.GetNameWithoutResolution(md.MediaFiles[toSkip].ResolvedFile), updatenSkipped: true); }).ConfigureAwait(false);
            }
            //
            if (null != b) b.IsEnabled = true;
        }
        //====================================================================================================================
        private void buttonPlay_Click(object sender, RoutedEventArgs e)
        {
            Button b = sender as Button;
            //
            if (null != b) b.IsEnabled = false;
            //
            if (wmp.IsPlaying())
            {
                stopPlaying();
            }
            else
            {
                if (0 == md.MediaFiles.Count())
                {
                    stopPlaying();
                }
                else
                {
                    playCurrent();
                }
            }
            //zbędne, playCurrent(); to zrobi
            //VideoClipNameHelper.CDBHandler.UpdateData(System.IO.Path.GetFileName(md.MediaFiles[currentPlaying].OriginalFile), updatenPlayed: true);            //
            if (null != b) b.IsEnabled = true;
        }
        //====================================================================================================================
        private void buttonNext_Click(object sender, RoutedEventArgs e)
        {
            Button b = sender as Button;
            //
            if (null != b) b.IsEnabled = false;
            int toSkip = currentPlaying;
            //
            if (0 != md.MediaFiles.Count())
            {
                if ((currentPlaying + 1) < md.MediaFiles.Count())
                {
                    wmp.VolumeToZero();
                    isBetweenTrack = true;
                    //
                    stopPlaying();
                    currentPlaying++;
                    playCurrent();
                    //
                    isBetweenTrack = false;
                    //wmp.VolumeToMaxAsync();
                }
                else
                {
                    stopPlaying();
                }
                System.Threading.Tasks.Task.Run(() => { return VideoClipNameHelper.CDBHandler.UpdateData(VideoClipData.GetNameWithoutResolution(md.MediaFiles[toSkip].ResolvedFile), updatenSkipped: true); }).ConfigureAwait(false);
            }
            //
            if (null != b) b.IsEnabled = true;
        }
        //====================================================================================================================
        private void win1_PreviewMouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (!wmp.IsPlaying())
            {
                return;
            }
            r2.IsEnabled = false;
            //
            if (this.WindowState != System.Windows.WindowState.Maximized)
            {
                VideoClipNameHelper.ScreenModes.EnterFullScreenMode(this);
            }
            else if (this.WindowState != System.Windows.WindowState.Normal)
            {
                VideoClipNameHelper.ScreenModes.LeaveFullScreenMode(this);
            }
            //
            r2.IsEnabled = true;
        }
        //====================================================================================================================
        private bool isRandom = false;
        private bool setRandom(bool isRandomNewValue)
        {
            bool currentRabdom = isRandom;
            if (isRandomNewValue)
            {
                isRandom = true;
                buttonRandom.ToolTip = "Random";
                SetButtonImage(buttonRandom, "Random");
            }
            else
            {
                isRandom = false;
                buttonRandom.ToolTip = "Sequential";
                SetButtonImage(buttonRandom, "NoRandom");
            }
            //
            return currentRabdom;
        }

        private void buttonRandom_Click(object sender, RoutedEventArgs e)
        {
            setRandom(!isRandom);
        }
        //====================================================================================================================
        private void progressBar1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (!wmp.IsPlaying())
            {
                return;
            }
            progressBar1.IsEnabled = false;
            wmp.VolumeToZero();
            Point p = e.GetPosition(progressBar1);
            wmp.SetProgress(p.X / (progressBar1.Maximum - progressBar1.Minimum));
            progressBar1.Value = (progressBar1.Maximum - progressBar1.Minimum) * (((double)wmp.Position.TotalSeconds / wmp.NaturalDuration.TimeSpan.TotalSeconds));
            VideoClipNameHelper.ScreenModes.DoEvents();
            wmp.VolumeToMaxAsync();
            progressBar1.IsEnabled = true;
        }
        //====================================================================================================================
        private void buttonDelete_Click(object sender, RoutedEventArgs e)
        {
            Button b = sender as Button;
            //
            if (null != b) b.IsEnabled = false;
            //
            int toDelete = currentPlaying;
            if (0 != md.MediaFiles.Count())
            {
                wmp.VolumeToZero();
                if ((currentPlaying + 1) < md.MediaFiles.Count())//ostatni
                {
                    isBetweenTrack = true;
                }
                //
                stopPlaying();
                //
                if ((currentPlaying + 1) < md.MediaFiles.Count())//ostatni
                {
                    currentPlaying++;
                    playCurrent();
                }
                //
                isBetweenTrack = false;
                //wmp.VolumeToMaxAsync();
                //
                System.Threading.Tasks.Task.Run(() => { return VideoClipNameHelper.CDBHandler.UpdateData(VideoClipData.GetNameWithoutResolution(md.MediaFiles[toDelete].ResolvedFile), isDeleted: true); }).ConfigureAwait(false);
                //
                deleteFileSafe(md.MediaFiles[toDelete].OriginalFile);
                if (md.MediaFiles[toDelete].OriginalFile != md.MediaFiles[toDelete].ResolvedFile) deleteFileSafe(md.MediaFiles[toDelete].ResolvedFile);
                //
            }
            //
            if (null != b) b.IsEnabled = true;
        }
        //====================================================================================================================
        private async void buttonNote_Click(object sender, RoutedEventArgs e)
        {
            Button b = sender as Button;
            //
            if (null != b) b.IsEnabled = false;
            //
            if (0 != md.MediaFiles.Count())
            {
                await OneLineTextLog.SaveMessage(cp, OneLineTextLog.LogFile.Note, md.MediaFiles[currentPlaying].ResolvedFile);
                //await saveReportMessage("notes.txt", md.MediaFiles[currentPlaying].ResolvedFile);
            }
            //
            if (null != b) b.IsEnabled = true;
        }
        //====================================================================================================================
        private async void buttonLike_Click(object sender, RoutedEventArgs e)
        {
            Button b = sender as Button;
            //
            if (null != b) b.IsEnabled = false;
            //
            if (0 != md.MediaFiles.Count())
            {
                await toggleLikeButtonState();
                //await OneLineTextLog.SaveMessage(cp, OneLineTextLog.LogFile.Like, md.MediaFiles[currentPlaying].ResolvedFile);
            }
            //
            if (null != b) b.IsEnabled = true;
        }

        private async System.Threading.Tasks.Task<bool> toggleLikeButtonState()
        {
            if (null == buttonLike.Tag) return false;
            //
            if (buttonLikeImage.Source == (Resources["NoLike"] as System.Windows.Media.ImageSource))
            {
                // IsLiked -> wyswietlamy `-' -> przełączamy na `+' i liked: false
                buttonLikeImage.Source = Resources["Like"] as System.Windows.Media.ImageSource;
                await VideoClipNameHelper.CDBHandler.UpdateData(buttonLike.Tag as string, isLiked: false);
            }
            else
            {
                // NoLiked -> wyswietlamy `+' -> przełączamy na `-' i liked: true
                buttonLikeImage.Source = Resources["NoLike"] as System.Windows.Media.ImageSource;
                await VideoClipNameHelper.CDBHandler.UpdateData(buttonLike.Tag as string, isLiked: true);
            }
            return true;
        }
        //====================================================================================================================
        //====================================================================================================================
    }//class
}//namespace
